/**
 * 
 */
package com.wolfpire.workflow.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wolfpire.system.common.EasyuiPage;
import com.wolfpire.system.common.EasyuiPagination;
import com.wolfpire.system.common.Page;
import com.wolfpire.system.model.Role;
import com.wolfpire.system.model.User;
import com.wolfpire.system.service.RoleService;
import com.wolfpire.system.service.UserService;
import com.wolfpire.workflow.em.ActorType;
import com.wolfpire.workflow.platform.PlatformService;

/**
 * 获取系统信息的控制器
 * @author lihd
 *
 */

@Controller
@RequestMapping("/platform")
public class PlatformController {
	
	@Resource private PlatformService platformService;
	
	@Resource private UserService userService;
	
	@Resource private RoleService roleService;
	
	@ResponseBody
	@RequestMapping(value = "roleTree", method = {RequestMethod.GET, RequestMethod.POST})
	public List<Role> getRoleTree() {
		return this.platformService.getRoleTree();
	}
	
	@ResponseBody
	@RequestMapping("/userList")
	public EasyuiPagination<User> userList(
				EasyuiPage<User> page, 
				@RequestParam(value = "id", required = false) Long id,
				@RequestParam(value = "type", required = false) String type,
				@RequestParam(value = "orgType", required = false) String orgType,
				@RequestParam(value = "nickName", required = false) String nickName
			) {
		if("org".equals(orgType)){
			
		} else if ("selectByRole".equals(orgType) && null == id) {
			/***/
			List<Role> roles = this.roleService.list(null);
			List<Long> roleIds = new ArrayList<Long>();
			for (Role role : roles) {
				roleIds.add(role.getId());
			}
			List<User> users = this.platformService.getUsersByNickNameAndRole(nickName, roleIds);
			page.setRows(1000000);
			page.setDataList(users);
			return new EasyuiPagination<User>(page);
		}  else if (!StringUtils.isBlank(nickName)) {
			User filterUser = new User();
			filterUser.setNickName(nickName);
			page.setRows(1000000);
			Page<User> users = this.userService.setPageDataList(page, filterUser);
			return new EasyuiPagination<User>(users);
		} else{
			List<User> users = new ArrayList<User>();
			if(id != null) {
				
				if(ActorType.ORG.getIndex().equals(type)) {//根据组织机构ID查询用户列表
					//platformService.queryUserPage(page, null, null, id);
//					userList = platformService.getUserListByOrgId(id);
					users = this.platformService.getUsersByOrgId(id);
				} else if(ActorType.ROLE.getIndex().equals(type)) {	//根据角色ID查询用户列表
//					userList = platformService.getUserListByRoleId(id);
					users = this.platformService.getUsersByRoleId(id);
				} 
				
			}
			page.setRows(1000000);
			page.setDataList(users);
			return new EasyuiPagination<User>(page);
		}
		
		return null;
	}
}
